package scau.zxck.service;

import scau.zxck.dao.ConnDao;
import scau.zxck.dao.TableInfoDao;
import scau.zxck.entity.ColumnInfo;
import scau.zxck.entity.TableInfo;
import scau.zxck.exception.BaseException;

import java.sql.Connection;
import java.util.List;

/**
 * Created by lsp on 27/11/2016.
 */
public class TableInfoService {
    private ConnDao connDao;

    public TableInfoService(String url, String username, String password) {
        connDao = new ConnDao(url, username, password);
    }

    public Boolean checkConn() {
        try {
            connDao.getConn();
        } catch (BaseException e) {
            return false;
        }
        return true;
    }

    public List<TableInfo> listTableInfo() {
        List<TableInfo> tableInfoList;
        try {
            Connection conn = connDao.getConn();
            TableInfoDao dao = new TableInfoDao(conn);

            tableInfoList = dao.getTableInfo();
            for (TableInfo tableInfo : tableInfoList) {
                List<ColumnInfo> columnInfoList = dao.getColumnInfo(tableInfo);
                tableInfo.setColumns(columnInfoList);
            }
        } catch (BaseException e) {
            e.printStackTrace();
            tableInfoList = null;
        }finally {
            try {
                connDao.close();
            } catch (BaseException e) {
                e.printStackTrace();
            }
        }
        return tableInfoList;
    }
}
